
local skynet = require 'skynet';
local string = require 'us.string';
local color = require 'color';
-- local config = require "system_config"

local string_format = string.format;

local log = {
	prefix = {
        nil,
        "Debug",
        nil,
		"Info",
		"Warning",
        "Error",
        nil,
    },
}

local level = 1;

function log.config(conf)
	level = conf.log_level
end

local function _print(priority, ...)
    if priority < level then return end

    local content;
    local tag = log.prefix[priority]
    if tag then
        content = string_format("[color(%s)][%s]%s", priority, tag, string.format(...));
    else
        content = string_format("[color(%s)]%s", priority, string.format(...));
    end
    -- local content = string.format("%s[%s][%s][%s]%s %s",
        -- log.color[priority], skynet.address(skynet.self()), os.date("%m/%d/%y %H:%M:%S", os.time()), tag, string.format(...), color.reset);


    -- local info = debug.getinfo(3);
    -- if priority < 3 then
    --     content = string.format("[%s]:%s", tag, string.format(...));
    -- else
    --     content = string.format("[%s][%s->line:%s]:%s", tag, info.short_src, info.currentline, string.format(...));
    -- end

    skynet.error(content);
end

function log.print(...)
    skynet.error(...)
end

function log.printf(...)
    _print(1, ...);
end

function log.debug(...)
    _print(2, ...);
end

function log.notice(...)
    _print(3, ...);
end

function log.info(...)
    _print(4, ...);
end


function log.warning(...)
    _print(5, ...);
end


function log.error(...)
    _print(6, "------------------------------------------------------------------------")
    _print(6, "%s%s", ..., debug.traceback("", 5));
    _print(6, "------------------------------------------------------------------------")
end


function log.fatal(...)
    _print(7, ...);
end


function log.traceback()
    _print(1, debug.traceback("", 5));
end


-- dump
local function dump_value_(v)
    if type(v) == "string" then
        v = "\"" .. v .. "\""
    end
    return tostring(v)
end


function log.dump(value, desc, nesting)
    if type(nesting) ~= "number" then
        nesting = 3;
    end

    local lookupTable = {}
    local result = {}

    local traceback = string.split(debug.traceback("", 1), "\n")
    _print(5, "dump from: " .. string.trim(traceback[3]))

    local function dump_(value, desc, indent, nest, keylen)
        desc = desc or "<var>"
        local spc = ""
        if type(keylen) == "number" then
            spc = string.rep(" ", keylen - string.len(dump_value_(desc)))
        end
        if type(value) ~= "table" then
            result[#result + 1] = string.format("%s[%s]%s = %s", indent, dump_value_(desc), spc, dump_value_(value))
        elseif lookupTable[tostring(value)] then
            result[#result + 1] = string.format("%s[%s]%s = *REF*", indent, dump_value_(desc), spc)
        else
            lookupTable[tostring(value)] = true
            if nest > nesting then
                result[#result + 1] = string.format("%s[%s] = *MAX NESTING*", indent, dump_value_(desc))
            else
                result[#result + 1] = string.format("%s[%s] = {", indent, dump_value_(desc))
                local indent2 = indent.."    "
                local keys = {}
                local keylen = 0
                local values = {}
                for k, v in pairs(value) do
                    keys[#keys + 1] = k
                    local vk = dump_value_(k)
                    local vkl = string.len(vk)
                    if vkl > keylen then keylen = vkl end
                    values[k] = v
                end
                table.sort(keys, function(a, b)
                    if type(a) == "number" and type(b) == "number" then
                        return a < b
                    else
                        return tostring(a) < tostring(b)
                    end
                end)
                for i, k in ipairs(keys) do
                    dump_(values[k], k, indent2, nest + 1, keylen)
                end
                result[#result +1] = string.format("%s}", indent)
            end
        end
    end
    dump_(value, desc, "- ", 1)

    for i, line in ipairs(result) do
        _print(2, line)
    end
end

-- log.config(config)

return log
